package com.itheima.mq.rabbitmq.consumer;

import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * Direct类型的交换机的消费者
 */
@Component
public class RabbitmqDirectExchangeConsumer {
    
    // 创建日志记录器
    private static final Logger LOGGER = LoggerFactory.getLogger(RabbitmqDirectExchangeConsumer.class);
    
    // @RabbitListener(queues = "direct.queue_01")
    // public void directExchangeQueue01(String messageBody) {
    //     LOGGER.info("direct exchange queue 01 message is : {}", messageBody);
    // }
    //
    @RabbitListener(queues = "direct.queue_02")
    public void directExchangeQueue02(String messageBody) {
        LOGGER.info("direct exchange queue 02 message is : {}", messageBody);
    }
    
    //消息手动应答
    @RabbitListener(queues = "direct.queue_01")
    public void directExchangeQueue01(String messageBody, Channel channel, Message message) {
        //手动应答,需要输入两个参数
        // 进行手动应答,第二个参数表示是否需要将该消息之前的所有的消息都进行应答
        try {
            LOGGER.info("manul response   queue 01 message is : {}", messageBody);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (IOException e) {
            e.printStackTrace();
            LOGGER.error("direct exchange queue 01 处理失败, message is :{}", messageBody);
        }
    }
}
